package com.grgbanking.it.system.dao.impl;



import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.grgbanking.it.base.BaseDao;
import com.grgbanking.it.system.dao.ISystemPropertyDao;
import com.grgbanking.it.system.entity.SystemProperty;


/**
 * 系统属性
 * 
 * SystemPropertyDao
 * 
 * @author 潘广伟
 * @Email p_3er@qq.com
 * @Date 2015-1-6 上午10:43:33
 * 
 * @version 1.0.0
 *
 */
@Repository 
public class SystemPropertyDao extends BaseDao<SystemProperty, String> implements ISystemPropertyDao<SystemProperty, String> { 
    @Autowired  
    public void setMySessionFactory(SessionFactory sessionFactory){  
        super.setSessionFactory(sessionFactory);  
    }
    
    /**
	 * 保存或更新
	 * saveOrUpdate
	 * 
	 * @param key
	 * @param value 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public void saveOrUpdate(String key, String value) {
		SystemProperty property = this.findById(key);
		if(property == null){
			property = new SystemProperty();
			property.setKey(key);
			property.setValue(value);
			this.save(property);
		}else{
			property.setValue(value);
			this.update(property);
		}
		
	}
	
	/**
	 * 获取值
	 * getValue
	 * 
	 * @param key
	 * @return 
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public String getValue(String key) throws Exception {
		StringBuffer sql = new StringBuffer("select t.value_ value from system_property t ");
		sql.append("where t.key_='").append(key).append("'");
		List<SystemProperty> list = this.findBySQL(sql.toString());
		String value = null;
		if(list.size() > 0){
			value = list.get(0).getValue();
		}
		return value;
	}

	
}